'use strict';

const Controller = require('egg').Controller;
const marked = require('marked');

/**
 * 评论相关api
 */
class CommentController extends Controller {
    
    //安装时的初始化
    async install() {
        const { ctx, service, config } = this;
        if ( ctx.app.config.env !== "local" ) {
            return ctx.helper.throwError(ctx, "没有操作权限");
        }

        let parameters = ctx.request.body;

        if ( !parameters.articleTitle ) {
            return ctx.helper.throwError(ctx, "缺少参数");
        }

        //查找默认评论
        let findCommentRes = await service.comment.findOne({
            mkContent: parameters.mkContent
        });

        if ( findCommentRes ) {
            return ctx.helper.throwError(ctx, "默认评论已存在");
        }

        //查找默认文章
        let findArticleRes = await service.article.findOne({
            title: parameters.articleTitle
        });

        if ( !findArticleRes ) {
            return ctx.helper.throwError(ctx, "默认文章不存在");
        }

        parameters.articleId = findArticleRes._id;
        delete parameters.articleTitle;

        if ( parameters.mdContent ) {
            parameters.htContent = marked(parameters.mdContent);
        }

        //创建默认评论
        let createCommentRes = await service.comment.create(parameters);

        if ( createCommentRes._id ) {
            //给文章增加评论数
            const updateArticleRes = await service.article.update(
                findArticleRes._id,
                {
                    $inc: { commentCount: Number(1) }
                }
            );
        }

        if ( createCommentRes._id ) {
            ctx.body = {
                code: 0,
                msg: "默认评论创建完成",
                data: createCommentRes
            }
        } else {
            return ctx.helper.throwError(ctx, "默认评论创建失败");
        }
    }

    //新增评论
    async create() {
        const { ctx, service, config } = this;
        let parameters = ctx.request.body;

        if ( !parameters.articleId ) {
            return ctx.helper.throwError(ctx, "缺少参数");
        }

        //查找所属文章
        let findArticleRes = await service.article.findOne({
            _id: parameters.articleId
        });

        if ( !findArticleRes ) {
            return ctx.helper.throwError(ctx, "文章不存在");
        }

        if ( parameters.mdContent ) {
            parameters.htContent = marked(parameters.mdContent);
        }

        //创建评论
        let createCommentRes = await service.comment.create(parameters);

        if ( createCommentRes._id ) {
            //给文章增加评论数
            const updateArticleRes = await service.article.update(
                findArticleRes._id,
                {
                    $inc: { commentCount: Number(1) }
                }
            );
        }

        if ( createCommentRes._id ) {
            ctx.body = {
                code: 0,
                msg: "评论创建完成",
                data: createCommentRes
            }
        } else {
            return ctx.helper.throwError(ctx, "评论创建失败");
        }
    }

    //更新评论
    async update() {
        const { ctx, service, config } = this;
        if ( !ctx.locals.currentUser.auth.isLogin ) {
            return ctx.helper.throwError(ctx, "你没有登陆", 101);
        }
        const id = ctx.request.body._id;
        let info = ctx.request.body;

        delete info._id;
        delete info.createTime;
        delete info.updateTime;

        if ( info.mdContent ) {
            info.htContent = marked(info.mdContent);
        }
        
        const updateRes = await service.comment.update(id, info);
        if ( updateRes ) { 
            ctx.body = {
                code: 0,
                msg: "更新成功",
                data: updateRes
            };
        } else {
            return ctx.helper.throwError(ctx, "更新失败");
        }
    }

    //删除评论
    async delete() {
        const { ctx, service, config } = this;
        if ( !ctx.locals.currentUser.auth.isLogin ) {
            return ctx.helper.throwError(ctx, "你没有登陆", 101);
        }

        const id = ctx.request.body.id;

        if ( !id ) {
            return ctx.helper.throwError(ctx, "参数错误");
        }

        const deleteRes = await service.comment.remove(id);

        if ( deleteRes ) {
            ctx.body = {
                code: 0,
                msg: "评论删除完成"
            }
        } else {
            return ctx.helper.throwError(ctx, "评论删除失败");
        }
    }

    //获取评论列表
    async list() {
        const { ctx, service, config } = this;
        if ( !ctx.locals.currentUser.auth.isLogin ) {
            return ctx.helper.throwError(ctx, "你没有登陆", 101);
        }
        let pageSize = Number(ctx.query.pageSize);
        let pageNumber = Number(ctx.query.pageNumber);
        pageSize = isNaN(pageSize) ? 10 : pageSize;
        pageNumber =  isNaN(pageNumber)? 0 : pageNumber;

        //获取评论列表
        const findCommentRes = await service.comment.find({}, pageNumber, pageSize);
        //获取文章总数
        const countCommentRes = await service.comment.count({});
        
        ctx.body = {
            code: 0,
            msg: '查询成功',
            data: {
                list: findCommentRes,
                total: countCommentRes
            }
        };
    }

    //获取单个评论信息
    async show() {
        const { ctx, service, config } = this;
        ctx.body = "hi!";
    }
}

module.exports = CommentController;